package
{
	import flash.display.Bitmap;
	import flash.display.GradientType;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import mx.managers.ISystemManager;
	import org.papervision3d.materials.BitmapAssetMaterial;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.MovieMaterial;
	
	import org.PaperBase.PaperBase;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;
	
		import caurina.transitions.*;
	
	public class Polaroid extends DisplayObject3D
	{
		//Rahmen
		private var topBorder:Plane;
		private var bottomBorder:Plane;
		private var leftBorder:Plane;
		private var rightBorder:Plane;
		//
		private var shadow:Plane;
		
		//Fresse
		protected var contentPlane:Plane;
		
		private var interactMode:Boolean = false;
		
		private var whiteMaterial:ColorMaterial;
		private var dummyMaterial:BitmapMaterial; 
		//Bilder
		[Embed(source='assets/mmss3d_1.jpg')]
		private static var dummystream1:Class;  
		[Embed(source = 'assets/mmss3d_2.jpg')]
		private static var dummystream2:Class;  
		[Embed(source = 'assets/mmss3d_3.jpg')]
		private static var dummystream3:Class;  
		[Embed(source = 'assets/mmss3d_4.jpg')]
		private static var dummystream4:Class;  
		[Embed(source = 'assets/mmss3d_5.jpg')]
		private static var dummystream5:Class;  
		[Embed(source = 'assets/mmss3d_6.jpg')]
		private static var dummystream6:Class; 
		[Embed(source = 'assets/mmss3d_7.jpg')]
		private static var dummystream7:Class; 
		[Embed(source = 'assets/mmss3d_8.jpg')]
		private static var dummystream8:Class; 
		[Embed(source = 'assets/mmss3d_9.jpg')]
		private static var dummystream9:Class;
		[Embed(source = 'assets/mmss3d_10.jpg')]
		private static var dummystream10:Class; 
		[Embed(source = 'assets/mmss3d_11.jpg')]
		private static var dummystream11:Class; 
		
		public function Polaroid(dummyContent:Boolean = true)
		{
			initPolaroid(dummyContent);
		}
		
		private function initPolaroid(dummyContent:Boolean):void{			
			//Bitmapmaertial erstellen
			var dummyBitmap:Bitmap;
			
			if(dummyContent){
				switch (Math.floor(Math.random() * 10 + 1))
				{
					case 1: dummyBitmap = new dummystream1() as Bitmap; break;
					case 2: dummyBitmap = new dummystream2() as Bitmap; break;
					case 3: dummyBitmap = new dummystream3() as Bitmap; break;
					case 4: dummyBitmap = new dummystream4() as Bitmap; break;
					case 5: dummyBitmap = new dummystream5() as Bitmap; break;
					case 6: dummyBitmap = new dummystream6() as Bitmap; break;
					case 7: dummyBitmap = new dummystream7() as Bitmap; break;
					case 8: dummyBitmap = new dummystream8() as Bitmap; break;
					case 9: dummyBitmap = new dummystream9() as Bitmap; break;
					case 10: dummyBitmap = new dummystream10() as Bitmap; break;
					case 11: dummyBitmap = new dummystream11() as Bitmap; break;
				} 
			}
			else {
				dummyBitmap = new dummystream11() as Bitmap;
			}
			dummyMaterial= new BitmapMaterial(dummyBitmap.bitmapData);
			dummyMaterial.doubleSided = true;
			
			whiteMaterial = new ColorMaterial(0xFFFFFF);
			whiteMaterial.name = "basic";
			whiteMaterial.doubleSided = true;
			
			this.topBorder = new Plane(whiteMaterial,1100,50,1,1);
			with(this.topBorder){
				x = 0;
				y = 0;
				parentContainer = this;
				name = "max";
			}
			
			this.leftBorder = new Plane(whiteMaterial,50,1250,1,1);
			with(this.leftBorder){
				y = -650;
				x = -525;
				parentContainer = this;
				name = "rotate1";
			}
			
			this.rightBorder = new Plane(whiteMaterial,50,1250,1,1);
			with(this.rightBorder){
				y = -650;
				x = 525;
				parentContainer = this;
				name = "rotate2";
			}
			
			this.bottomBorder = new Plane(whiteMaterial,1000,250,1,1);
			with(this.bottomBorder){
				y = -1150;
				x = 0;
				parentContainer = this;
				name = "drag2";
			}
			
			this.contentPlane = new Plane(dummyMaterial,1000,1000,10,10);
			with (this.contentPlane) {
				y = -525;
				x = 0;
				parentContainer = this;
				name = "content"; 
			}
			
			//Verlauf erstellen
			var matr:Matrix = new Matrix();
			var verlauf:Sprite = new Sprite();
			//
			matr.createGradientBox(1100,250,Math.PI/2, 0, 0);
            verlauf.graphics.beginGradientFill(GradientType.LINEAR,[0xffffff,0x000000],[0.5, 0],[0,255], matr, SpreadMethod.PAD);
			verlauf.graphics.drawRect(0, 0, 1100, 250);
			var tmpMat:MovieMaterial = new MovieMaterial(verlauf, true);
			tmpMat.doubleSided = true;
			this.shadow = new Plane(tmpMat);
			with(this.shadow){
				y = -1405;
				x = 0;
				parentContainer = this;
				name = "shadow";
			}

			
			
			this.addChild(topBorder);
			this.addChild(rightBorder);
			this.addChild(leftBorder);
			this.addChild(bottomBorder);
			this.addChild(shadow);
			this.addChild(contentPlane);
			
			//mmss_3D.setListener(this);
			
			
		}
		
		protected function enableUpdate():void{
			PaperBase.getEventManager.setListener(this,update);
		}
		
		protected function disableUpdate():void{
			PaperBase.getEventManager.removeListener(this,update);
		}
		
		public function rotateIt(tmpX:Array):void{
			this.rotationY += ((tmpX[0] / tmpX[1] - 0.5) * 5);
		}
		
		public function disableInteractive():void {
			trace("Setze inaktiv");
			this.whiteMaterial.interactive = false;
			this.dummyMaterial.interactive = false;
		}
		
		public function enableInteractive():void {
			trace("Setze aktiv");
			this.whiteMaterial.interactive = true;
			this.dummyMaterial.interactive = true;
		}
		
		public function dragIt(fx:Number, fz:Number, fy:Number):void{
			if(fz < 10000  && fy > -500 && fz > -1400){
				this.z -= ((this.z - fz));
				this.y -= ((this.y - fy -1250));
				this.x -= ((this.x - fx));
			}
		}
		
		public function update():void {
			//Nichts zu tun
		}
		
		public function scroll(value:int,ctrlKey:Boolean):void {
			//Nichts zu tun
		}
		public function tweenMe():void {
			
			Tweener.addTween(this, {
					x: Math.random() * 5000 - 1000,
					z: Math.random() * 15000  -500,
					rotationY: 360,
					time:2,
					delay: Math.random() * 5,
					transition:"easeInOutBack",
					onComplete: function():void {
									this.rotationY = 0;
									this.tweenMe();
								}
					});
			
		}
		
	}
}